package interview.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 锁绑定多个条件的Condition
 *A线程打印5次，----->通知B线程打印10次---->通知C线程打印15次数
 *
 *
 * */

class DataReSource{

    private  int number=1;
    Lock lock = new ReentrantLock();
    Condition c1 = lock.newCondition();
    Condition c2 = lock.newCondition();
    Condition c3 = lock.newCondition();

     public  void print5(){
         try {
             Thread.sleep(200);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
         lock.lock();
         System.out.println("线程A拿到锁");
         try {
             while (number != 1) {
                c1.await();
             }

             for (int i=1;i<=5;i++){
                 System.out.println(Thread.currentThread().getName()+":"+i);
             }
             number=2;
             c2.signal();
         } catch (InterruptedException e) {
             e.printStackTrace();
         } finally {
             lock.unlock();
             System.out.println("线程A释放锁");
         }

     }

    public  void print10(){
        lock.lock();
        System.out.println("线程B拿到锁");
        try {
            while (number != 2) {
                c2.await();
            }

            for (int i=1;i<=10;i++){
                System.out.println(Thread.currentThread().getName()+":"+i);
            }
            number=3;
            c3.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
            System.out.println("线程B释放锁");
        }

    }
    public  void print15(){
        lock.lock();
        System.out.println("线程C拿到锁");
        try {
            while (number != 3) {
                c3.await();
            }

            for (int i=1;i<=15;i++){
                System.out.println(Thread.currentThread().getName()+":"+i);
            }
            number=1;
            c1.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
            System.out.println("线程C释放锁");
        }
    }


    public void test(){
         try {

             lock.lock();
             System.out.println(Thread.currentThread().getName()+"获得锁");
             Thread.sleep(200);
             for(int i=0;i<10;i++){
                 System.out.println(Thread.currentThread().getName()+"do.....");
             }
         }catch (Exception e){
             e.printStackTrace();
         }finally {
             lock.unlock();
             System.out.println(Thread.currentThread().getName()+"释放锁");
         }


    }

}

public class MultiCondition {

    public static void main(String[] args) {



        DataReSource dataReSource = new DataReSource();
        for(int i=0;i<100;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    dataReSource.test();

                }
            }).start();
        }
        /*new Thread(()->{
            for(int i=1;i<10;i++){
                dataReSource.print5();
            }
        },"A").start();

        new Thread(()->{
            for(int i=1;i<10;i++){
                dataReSource.print10();
            }
        },"B").start();

        new Thread(()->{
            for(int i=1;i<10;i++){
                dataReSource.print15();
            }
        },"C").start();*/
    }
}
